// vuex的状态机,根据功能模块划分为设备管理，设备点检，设备维修，设备保养等
import {
    fetchRolePage,
    fetchCurrentOrgInfo,
    saveRightOrgInfo,
    fetchMenuList,
    fetchRoleList,
    fetchRightGroups,
    fetchRightMenuList,
    fetchRightUserList,
    saveRightGroups,
    delRightGroups
} from "@/api/system-right/index";
import { reduceNullContent, deepClone } from "@/utils/index";
import data from "@/utils/constData";
const optionData = function(e) {
    if (e) {
        let cloneData = JSON.parse(JSON.stringify(e)); // 对源数据深度克隆

        return cloneData.filter(father => {
            // 循环所有项，并添加children属性
            let branchArr = cloneData.filter(child => father.id == child.parentId); // 返回每一项的子级数组

            branchArr.length > 0 ? (father.children = branchArr) : ""; //给父级添加一个children属性，并赋值
            return father.parentId == 0 && father.hidden != 1; //返回第一层
        });
    }
};
const state = {
    menuList: [],
    roleList: [],
    rolePage: [],
    // chooseRecords: [],
    groups: [],
    //---------数据权限-------------//
    rightMenuList: [],
    listSearchParams: {},
    rightTree: [],
    currentItem: {},
    //查询的关联部门
    rightOrganIds: [],
    pages: 1,
    total: 20,
    total1: 10
};
const mutations = {
    SET_LISTSERACH_PARAMS: (state, args) => {
        state.listSearchParams = args;
    },
    SET_LISTDATA: (state, args) => {
        state.pages = args.pages;

        state.total = args.total;
    },
    SET_MENULIST: (state, args) => {
        state.menuList = args;
    },
    SET_ROLELIST: (state, args) => {
        state.roleList = args;
    },
    // SET_CHOOSERECORDS: (state, args) => {
    //     state.chooseRecords = args
    // },
    SET_GROUPS: (state, args) => {
        state.groups = args;
    },
    SET_RIGHTMENULIST: (state, args) => {
        state.rightMenuList = args;
    },
    SET_LISTSEARCHPARAMS: (state, args) => {
        state.listSearchParams = args;
    },
    SET_RIGHTTREE: (state, args) => {
        state.rightTree = args;
    },
    SET_CURRENTITEM: (state, args) => {
        state.currentItem = args;
    },
    SET_RIGHTORGANIDS: (state, args) => {
        state.rightOrganIds = args;
    },
    // REMOVE_USERS: (state, args) => {
    //     state.chooseRecords = args
    // },

    SET_ROLEPAGE: (state, args) => {
        state.rolePage = args;
    }
};

const actions = {
    // 查询参数
    setListSearchParams({ commit, state }, args) {
        const data = Object.assign({}, state.listSearchParams, args);
        console.log(data, "++++data++++");
        commit("SET_LISTSERACH_PARAMS", data);
    },
    //菜单列表
    requestMenuList({ commit, state }, args) {
        return fetchMenuList(reduceNullContent(args)).then(response => {
            console.log("设备台账", optionData(response.data));
            const data = optionData(response.data);
            commit("SET_MENULIST", data);
        });
    },
    //角色列表
    requestRoleList({ commit, state }, args) {
        return fetchRoleList(reduceNullContent(args)).then(response => {
            console.log("角色", response.data);
            const data = response.data;
            data.forEach(e => {
                e.label = e.realName + `(${e.positionsName})`;
                e.value = e.userId //JSON.stringify(e);
            });
            debugger
            commit("SET_ROLELIST", data);
        });
    },
    //分页查询角色列表
    requestRolePage({ commit, state }, args) {
        return fetchRolePage(reduceNullContent(args)).then(response => {
            console.log("角色", response.data);
            const data = response.data.records;

            state.total1 = response.data.total;
            commit("SET_ROLEPAGE", data);
        });
    },
    // setChooseRecords({ commit, state }, args) {
    //     commit("SET_CHOOSERECORDS", args)
    // },
    //获取权限组信息
    requestRightGroups({ commit, state }, args) {
        return fetchRightGroups(reduceNullContent(args)).then(response => {
            console.log("权限组", response.data);
            const data = response.data.map(e => {
                e.title = e.groupsName;
                e.name = e.groupsName;
                // e.key = e.id
                return e;
            });

            commit("SET_GROUPS", data);
        });
    },
    //通过主键获取权限数据
    requestRightMenuList({ commit, state }, args) {
        return fetchRightMenuList(reduceNullContent(args));
        //.then((response) => {
        // console.log("权限组", response.data)
        // const data = response.data.map(e => {
        //     e.title = e.groupsName
        //     e.name = e.groupsName
        //     e.key = e.id
        //     return e
        // })

        //commit("SET_RIGHTMENULIST", data)
        //});
    },
    //通过主键获取用户数据
    requestRightUserList({ commit, state }, args) {
        return fetchRightUserList(args);
    },
    //保存权限组
    saveGroups({ commit, state }, args) {
        return saveRightGroups(args);
    },
    //删除权限组
    delGroups({ commit, state }, args) {
        console.log(args, "++++args+++");
        return delRightGroups(args.id).then(res => {
            const data = args.groups.map(e => {
                return e.id != args.id;
            });
            console.log(data, "+++data+++");
            commit("SET_GROUPS", data);
        });
    },
    // setListSearchParams({ commit, state }, args) {
    //   commit("SET_LISTSEARCHPARAMS", args);
    // },
    //设置选择得树
    rightTree({ commit, state }, args) {
        commit("SET_RIGHTTREE", args);
    },
    //保存相关账号的部门
    saveRightOrg({ commit, state }, args) {
        return saveRightOrgInfo(args);
    },
    //点击当前选项查询相关部门
    setCurrentItem({ commit, state }, args) {
        commit("SET_RIGHTORGANIDS", []);
        return fetchCurrentOrgInfo(args.id)
            .then(res => {
                commit("SET_CURRENTITEM", args); //当前选择的数据
                const rightOrgIds = res.data ?
                    res.data.map(e => {
                        return e.organId;
                    }) : [];
                commit("SET_RIGHTORGANIDS", rightOrgIds);
            })
            .catch(err => {});
    },
    //设置当前数据的关联部门
    setRightOrganIds({ commit, state }, args) {
        commit("SET_RIGHTORGANIDS", args);
    }
    // removeUser({ commit, state }, args) {

    //     const data = state.chooseRecords.filter(e => {
    //         // if (e.userId != args.userId) {
    //         //     return e
    //         // }
    //         return e.userId != args.userId
    //     })
    //     commit("REMOVE_USERS", data)
    // }
};

export default {
    namespaced: true,
    state,
    mutations,
    actions
};